const { override, fixBabelImports, addLessLoader, addWebpackAlias, addPostcssPlugins, addBabelPlugin, addBundleVisualizer, addWebpackPlugin, overrideDevServer } = require('customize-cra');
const path = require("path");
const webpack = require('webpack')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
// const px2rem = require('postcss-px2rem-exclude');
// px2rem({remUnit:75,exclude: /node_modules/i}),

const CompressionWebpackPlugin = require('compression-webpack-plugin');

const addCustomize = () => config => {
    if (process.env.NODE_ENV === 'production') {
        // 关闭sourceMap
        config.devtool = false;
        // 配置打包后的文件位置
        // config.output.path = resolve('dist');
        // config.output.publicPath = './';
        // 添加js打包gzip配置
        config.plugins.push(
            new CompressionWebpackPlugin({
                test: /\.js$|\.css$/,
                threshold: 1024,
            }),
            // 将 dll 注入到 生成的 html 模板中
            new AddAssetHtmlPlugin({
                // dll文件位置
                filepath: path.resolve(__dirname, './public/vendor/*.js'),
                // dll 引用路径
                publicPath: '/vendor',
                // dll最终输出的目录
                outputPath: '/vendor'
            }),
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./public/vendor/vendor-manifest.json'),
                name: 'vendor'
            })
        )
    }
    return config;
}

// dev 配置 暂时不使用
const devServerConfig = () => config => {
    return {
        ...config,
        proxy: {
            '/api': {
                target: 'xxx',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '/api',
                },
            }
        }
    }
}


process.env.GENERATE_SOURCEMAP = "false";

module.exports = override(
    fixBabelImports('import', { // Ant 按需
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: true,
    }),

    addWebpackAlias({ // 路径别名
        '@': path.resolve(__dirname, 'src'),
    }),

    addLessLoader({ // less
        javascriptEnabled: true,
        modifyVars: {
            '@primary-color': '#1DA57A'
        },
    }),

    addPostcssPlugins([
        require('postcss-pxtorem')({ rootValue: 75, propList: ['*'], minPixelValue: 2, selectorBlackList: ['am-'] })
    ]), // lib-flexible 接入配置

    addCustomize()
);